<template>
  <div class="production-plan">
    <el-button type="primary" @click="saveAll">保存主表单和明细</el-button>
    <h3 class="title">专检</h3>
    <!-- 基本信息 -->
    <el-form ref="formRef" :model="formData" :rules="rules" label-position="right" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="工单号">
            <el-input v-model="formData.workOrderNumber" placeholder="请输入工单号" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工单类型" prop="workOrderType">
        <el-select v-model="formData.workOrderType" placeholder="请选择工单类型" >
          <el-option label="糖化工单" value="糖化工单"></el-option>
          <el-option label="发酵工单" value="发酵工单"></el-option>
          <el-option label="过滤工单" value="过滤工单"></el-option>
          <el-option label="包装工单" value="包装工单"></el-option>    
        </el-select>
      </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="产线">2
            <el-input v-model="formData.productionLine" placeholder="请输入产线" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="物料编号">
            <el-input v-model="formData.materialCode" placeholder="请输入物料编号" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="物料名称">
            <el-input v-model="formData.materialName" placeholder="请输入物料名称" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 物料明细明细 -->
    <div class="detail-section">
      <h4 class="sub-title">物料明细</h4>
      <el-button size="small" @click="addNewRow">新增行</el-button>
      <el-table :data="planDetailsList" border style="width: 100%">
        <el-table-column label="前发酵罐号">
          <template #default="{ row }">
            <el-input v-model="row.frontFermentationTank" :readonly="row.isSaved"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="后发酵罐号">
          <template #default="{ row }">
            <el-input v-model="row.rearFermentationTank" :readonly="row.isSaved"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="清酒罐号">
          <template #default="{ row }">
            <el-input v-model="row.sakeTank" :readonly="row.isSaved"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="满罐时间">
          <template #default="{ row }">
            <el-input-number v-model.number="row.fullTankTime" :min="0" controls-position="right"
              :disabled="row.isSaved"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="采样点">
          <template #default="{ row }">
            <el-input-number v-model.number="row.samplingPoint" :min="0" controls-position="right"
              :disabled="row.isSaved"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="检验结果">
          <template #default="{ row }">
            <el-input v-model="row.inspectionResult" :readonly="row.isSaved"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="提交时间">
          <template #default="{ row }">
            <el-input v-model="row.submitTime" :readonly="row.isSaved"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-input-number v-model.number="row.status" :min="0" controls-position="right"
              :disabled="row.isSaved"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180px">
          <template #default="{ row }">
            <!-- 移除单独保存明细按钮 -->
            <el-button size="small" type="danger" @click="deleteDetail(row)" :disabled="row.isSaved">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>


<script setup>
import { ref, reactive, onMounted, watch } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter, useRoute } from 'vue-router';
import { Materialread, Materialwrite } from '@/axios/axiosHelper';
import axios from 'axios';
import { th } from 'element-plus/es/locales.mjs';

const formRef = ref(null);
const currentUser = ref("admin");
const router = useRouter();
const route = useRoute();

  
const formData =reactive({
  id:'',
  workOrderNumber: '',
  workOrderType: '',
  productionLine: '',
  materialCode: '',
  materialName: '',
 
})
          
const saveAll = () => {
  if (formRef.value) {
    const isValid = formRef.value.validate();
    if (isValid) {
      try {
        // 保存主表单
        const response =  Materialwrite.post('/l-write/api/Ment/AddMaterials', formData);
        if (response.data.code === 100) {
          formData.id = response.data.data.id;
          ElMessage.success('主表单保存成功');

          // 保存明细
          const saveDetailPromises = planDetailsList.value.map((row) => {
            const detailWithId = {
              ...row,
              id: formData.id
            };
            const detailResponse =  Materialwrite.post(
              '/l-write/api/Ment/AddMaterialsChild',
              detailWithId
            );
            if (detailResponse.data.code == 100) {
              row.isSaved = true;
              return true;
            } else {
              ElMessage.error(`明细保存失败：${detailResponse.data.message}`);
              return false;
            }
          });

          const results = Promise.all(saveDetailPromises);
          if (results.every(result => result)) {
            ElMessage.success('所有明细保存成功');
            router.push('/GetMaterials');
          } else {
            ElMessage.error(`主表单保存失败：${res.data.message}`);
          }
        }
      } catch (error) {
        console.error('保存过程中发报错公婆你好标点符号发生错误生错误:', error.response?.data);
        ElMessage.error('保存过程中发生错误，请检查网络或联系管理员');
      }
    }
  }
};


// 新增行
const addNewRow = () => {
  planDetailsList.value.push({
    frontFermentationTank: '',
  rearFermentationTank: '',
  sakeTank: '',
  fullTankTime: '',
  samplingPoint: '',
  inspectionResult: '',
  submitTime: '',          
  status: '',

  });
};

  </script>
   
  <style scoped>
  /* 可以在此处添加样式 */
  </style>